/**
 *
 */
package com.jdy.haoduoai.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoai.enums.SelectionMode;
import com.jdy.haoduoai.model.PainPointItem;
import com.jdy.haoduoai.util.HttpUtils;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-11-29 上午9:59:38
 */
public class PainPointCustomAdapter extends BaseAdapter {

    private final Context mContext;

    private final LayoutInflater mInflater;

    private final SparseArray<View> mSpArray;

    private ArrayList<PainPointItem> mPainPointItemList;

    private OnPainPointSelectListener mOnPainPointSelectListener;
    private boolean isEditor = false;
    private ArrayList<Integer> mSelectedList;

    public SelectionMode SelectMode = SelectionMode.NONE;


    public void setSelectMode(SelectionMode sm) {
        SelectMode = sm;

        if (mPainPointItemList != null && !mPainPointItemList.isEmpty()) {
            if (SelectMode == SelectMode.SELECT_ALL) {
                for (PainPointItem item : mPainPointItemList) {
                    item.isChecked = true;
                }
            } else if (SelectMode == SelectMode.DESELECT_ALL) {
                for (PainPointItem item : mPainPointItemList) {
                    item.isChecked = false;
                }
            }
        }
    }

    public void setEditor(boolean iseditor) {
        this.isEditor = iseditor;
    }

    public PainPointCustomAdapter(Context ctx, ArrayList<PainPointItem> list) {
        this.mContext = ctx;
        this.mPainPointItemList = list;
        this.mInflater = LayoutInflater.from(mContext);
        this.mSpArray = new SparseArray<>();
        this.mSelectedList = new ArrayList<>();
    }

    public void setData(ArrayList<PainPointItem> list) {
        mPainPointItemList = list;
    }

    public void setSelectedList(ArrayList<Integer> list) {
        this.mSelectedList = list;
    }

    public void setPainPointSelectListener(OnPainPointSelectListener listener) {
        this.mOnPainPointSelectListener = listener;
    }


    public void remove(PainPointItem object) {
        mPainPointItemList.remove(object);
        notifyDataSetChanged();
    }

    public ArrayList<PainPointItem> getList() {
        return mPainPointItemList;
    }

    public void removeSelection() {
        for (PainPointItem item : mPainPointItemList) {
            item.isChecked = false;
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mPainPointItemList != null)
            return mPainPointItemList.size();
        else
            return 0;
    }

    @Override
    public PainPointItem getItem(int position) {
        if (mPainPointItemList != null)
            return mPainPointItemList.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        if (mPainPointItemList != null)
            return position;
        else
            return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final PainPointItem ppi = mPainPointItemList.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.custom_habit_item, null);
            holder.img = convertView
                    .findViewById(R.id.img);
            holder.tv_clock = convertView.findViewById(R.id.tv_clock);
            holder.tv_morning = convertView.findViewById(R.id.tv_morningsong);
            holder.selectCb = convertView.findViewById(R.id.select_cb);
            holder.radiobtn = convertView.findViewById(R.id.radiobtn);
            holder.rela_parent = convertView
                    .findViewById(R.id.rela_parent);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ViewHolder holder2 = holder;

        holder.tv_morning.setText(ppi.name);

        if (ppi.img.contains("api.jdy520.com") || ppi.img.contains("api.lexiaobao.net")) {
            Picasso.with(mContext).load(ppi.img).into(holder.img);
        } else {
            Picasso.with(mContext).load(HttpUtils.HOST_RESOURCE + ppi.img).into(holder.img);
        }

        int lastIdx = ppi.time.lastIndexOf(":");
        holder.tv_clock.setText(ppi.time.substring(0, lastIdx));

        //选中的监听事件
        holder.selectCb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mOnPainPointSelectListener != null) {
                    mOnPainPointSelectListener.onPainPointSelect(ppi.id, isChecked);
                }

                if (isChecked) {
                    ppi.enable = 1;
                } else {
                    ppi.enable = 0;
                }
            }
        });

        holder.selectCb.setChecked(ppi.enable == 1);

        if (isEditor) { // 处于编辑形态
            holder.radiobtn.setVisibility(View.VISIBLE);
            holder.selectCb.setVisibility(View.GONE);
        } else {
            holder.radiobtn.setVisibility(View.GONE);
            holder.selectCb.setVisibility(View.VISIBLE);
        }

        if (isEditor) {
            holder.rela_parent.setClickable(true);
            holder.rela_parent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.radiobtn.setChecked(!holder.radiobtn.isChecked());
                    mPainPointItemList.get(position).isChecked = holder.radiobtn.isChecked();
                }
            });
        } else {
            holder.rela_parent.setClickable(false);
        }

        holder.radiobtn.setChecked(mPainPointItemList.get(position).isChecked);

        return convertView;
    }

    private static class ViewHolder {
        ImageView img;
        TextView tv_clock;
        TextView tv_morning;
        CheckBox selectCb;
        private CheckBox radiobtn;
        private RelativeLayout rela_parent;
        View slayout;
    }

}
